﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.CommonMethods;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmEmployee : Form
    {
        User Gvuser = new User();
        List<string> GvSectionChecked = new List<string>();

        public frmEmployee()
        {
            InitializeComponent();
        }

        #region Employee Save
        enum EmpAccessability
        {
            [Description("tabInventory")]
            Inventory_Center,
            [Description("stabItems")]
            T1,
            [Description("sstabItem")]
            ST1,
            [Description("sstabPackage")]
            ST2,
            [Description("stabStock")]
            T2,
            [Description("stabCategory")]
            T3,
            [Description("sstabMaincat")]
            ST3,
            [Description("sstabSubcat")]
            ST4,
            [Description("stabTaxes")]
            T4,
            [Description("stabDiscount")]
            T5,
            [Description("stabStockAdjust")]
            T6,
            [Description("stabStockTransfer")]
            T7,
            [Description("sstabStkTransferReq")]
            ST5,
            [Description("sstabStktransfernote")]
            ST6,
            [Description("stabDisposal")]
            T8,
            [Description("stabPurchase")]
            T9,
            [Description("sstabPR")]
            ST7,
            [Description("sstabPO")]
            ST8,
            [Description("sstabGRN")]
            ST9,
            [Description("sstabPRN")]
            ST10,
            [Description("tabCustomer")]
            Customer_Center,
            [Description("stabCusreturn")]
            T10,
            [Description("stabCusRegistation")]
            T11,
            [Description("tabVendor")]
            Vendor_Center,
            [Description("stabVendorList")]
            T12,
            [Description("stabPO")]
            T13,
            [Description("stabVendorBill")]
            T14,
            [Description("stabVpay")]
            T15,
            [Description("tabEmployee")]
            Employee,
            [Description("stabEmp")]
            T16,
            [Description("sstabInfo")]
            ST11,
            [Description("sstabSection")]
            ST12,
            [Description("sstabIntersection")]
            ST13,
            [Description("stabChngpass")]
            T17,
            [Description("tabApproval")]

            Approval,
            [Description("stabPurchaseOrder")]
            T18,
            [Description("stabGRN")]
            T19,
            [Description("stabPR")]
            T20,
            [Description("stabSTN")]
            T21,
            [Description("tabFranchisor")]

            Franchisor,
            [Description("stabAddFran")]
            T22,
            [Description("stabFranpay")]
            T23
        }
        enum SectionAccessability
        {
            [Description("btnPOS")]
            chkPOS,
            [Description("btnManagement")]
            chkManagement,
            [Description("btnStatic")]
            chkStatic,
            [Description("btnSetup")]
            chkSetup,

        }
        private List<string> CollectSectionchecked()
        {
            List<string> Lvsection = new List<string>();
            string Lvstr;
            if (chkPOS.Checked)
            {
                Lvstr = "chkPOS";
                GvSectionChecked.Add(Lvstr);
            }

            if (chkManagement.Checked)
            {
                Lvstr = "chkManagement";
                GvSectionChecked.Add(Lvstr);
            }

            if (chkStatic.Checked)
            {
                Lvstr = "chkStatic";
                GvSectionChecked.Add(Lvstr);
            }

            if (chkSetup.Checked)
            {
                Lvstr = "chkSetup";
                GvSectionChecked.Add(Lvstr);
            }

            foreach (string item in GvSectionChecked)
            {
                Lvsection.Add(EnumHelper<EmpAccessability>.GetEnumDescription(item));
            }
            return Lvsection;

        }
        private List<string> CollectCheckedAccess()
        {
            try
            {
                string LvName;
                List<string> Lvchknodes = new List<string>();
                List<string> LvEnum = new List<string>();


                foreach (TreeNode currentNode in tvintersection.Nodes)
                {
                    if (currentNode.Checked)
                    {

                        Lvchknodes.Add(currentNode.Name.ToString());
                    }

                }


                foreach (string item in Lvchknodes)
                {
                    foreach (string str in Enum.GetNames(typeof(EmpAccessability)))
                    {
                        if (item == str)
                        {
                            LvName = EnumHelper<EmpAccessability>.GetEnumDescription(str);
                            LvEnum.Add(item);
                        }
                    }
                }
                return LvEnum;
            }
            catch (Exception)
            {
                return null;
            }

        }

        private User CollectUserFigures()
        {
            string Lvaccess = "";
            try
            {
                User Lvuser = new User();
                Lvuser.Date_Create = System.DateTime.Now;
                Lvuser.Last_Modify = System.DateTime.Now;
                Lvuser.Pas_Word = txtPassword.Text;
                Lvuser.Use_Id = Gvuser.Use_Id;
                Lvuser.Use_Name = txtUsername.Text;
                Lvuser.Use_Type = cmbType.Text;
                foreach (string item in CollectCheckedAccess())
                {
                    Lvaccess += item;
                }
                foreach (string itemnav in CollectSectionchecked())
                {
                    Lvaccess += itemnav;
                }
                Lvuser.User_Access = Lvaccess;
                Lvuser.User_Id = txtUserID.Text;
                Lvuser.User_Modify = Gvuser.Use_Name;
                return Lvuser;
            }
            catch (Exception)
            {
                return null;
            }

        }
        private void btnSaveEmp_Click_1(object sender, EventArgs e)
        {

            SaveEmployeeANDUser();

            MessageBox.Show("EMPLOYEE WAS BSUCCESFULLY SAVED");
        }
        private int SaveEmployeeANDUser()
        {

            Employee LvEmployee = CollectEmployeeFigures();
            User Lvuser = CollectUserFigures();
            if (LvEmployee != null && Lvuser != null)
            {
                return EmployeeADO.SaveEmployee(LvEmployee, Lvuser);

            }
            else
            {
                return 0;
            }
        }
        private Employee CollectEmployeeFigures()
        {

            try
            {
                Employee LvEmployee = new Employee();
                LvEmployee.Address2 = rtxtAddress.Text;
                LvEmployee.City = txtCity.Text;
                LvEmployee.Country = txtCountry.Text;
                LvEmployee.Date_Create = System.DateTime.Now;
                LvEmployee.Dob = dtpBirthday.Text;
                LvEmployee.Email = txtEmail.Text;
                LvEmployee.Emp_ID = txtEmpid.Text;
                LvEmployee.Giv_Name = txtFname.Text;
                LvEmployee.Hom_Tel_No = txtPhone.Text;
                LvEmployee.Mob_No = txtMobile.Text;
                LvEmployee.Postal_Code = txtPostalCode.Text;
                LvEmployee.State = txtstate.Text;
                LvEmployee.Sur_Name = txtLname.Text;
                LvEmployee.Title = cmdTitle.Text;
                LvEmployee.User_Id = "1";
                LvEmployee.User_Modify = Gvuser.Use_Name;
                LvEmployee.Last_Modify = System.DateTime.Now;
                return LvEmployee;


            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region Employee New

        private void btnNewemp_Click(object sender, EventArgs e)
        {
            txtUserID.Text = UserDAO.GetNewUser();
        }

        #endregion

        #region Employee Change Password

        private void btnChngPW_Click_1(object sender, EventArgs e)
        {
            User Lvuser = CollectchngPWfigures();
            int LVresult = UpdateUser();
            if (LVresult == 1)
            {
                MessageBox.Show("PASSWORD WAS SUCCESFULLY UPDATED");
            }
            else
            {
                MessageBox.Show("TRY AGAIN");
            }
        }


        private User CollectchngPWfigures()
        {
            User Lvuser = new User();
            Lvuser.Last_Modify = System.DateTime.Now;
            Lvuser.Pas_Word = txtNewPW.Text;
            Lvuser.User_Modify = "1";
            Lvuser.Use_Name = txtChngUserName.Text;
            return Lvuser;

        }

        private int UpdateUser()
        {
            try
            {
                User Lvuser = CollectchngPWfigures();
                string Lvoldpw = txtCurrentPW.Text;
                string Lvnewpw = txtNewPW.Text;
                string Lvnewre = txtRePW.Text;
                string LvUsername = txtChngUserName.Text;
                string Lvusername;
                string LvPW = null;

                List<User> LvList = UserDAO.GetUserByUsername(LvUsername);
                foreach (User item in LvList)
                {
                    LvPW = item.Pas_Word;
                    Lvusername = item.Use_Name;
                }

                if (LvPW == Lvoldpw)
                {
                    if (Lvnewpw == Lvnewre)
                    {

                        if (Lvuser != null)
                        {
                            return UserDAO.UpdateUser(Lvuser);


                        }
                        else
                        {
                            return 0;
                        }

                    }
                    else
                    {

                        MessageBox.Show("Password Mismatch");
                        return 0;
                    }
                }
                else
                {
                    MessageBox.Show("Wrong Password");
                    return 0;
                }


            }
            catch (Exception)
            {
                return 0;
            }
        }

        private void btnChngCancel_Click_1(object sender, EventArgs e)
        {
            txtChngUserName.Text = "";
            txtNewPW.Text = "";
            txtCurrentPW.Text = "";
            txtRePW.Text = "";
        }
        #endregion

      

        
       

        

       
       

    }
}
